﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyDoanVien.Bussiness;
using QuanLyDoanVien.Controller;
using DevComponents.DotNetBar.Controls;

namespace QuanLyDoanVien
{

    public partial class frmQLThamGia : Form
    {
        public frmQLThamGia()
        {
            InitializeComponent();
        }
        #region Biến

        List<Diem> dsDiem = new List<Diem>();
        List<HoatDongInfo> dsHD = new List<HoatDongInfo>();
        List<HoatDongHocSinhInfo> dsHDHS = new List<HoatDongHocSinhInfo>();

        HoatDongCtrl m_HoatDongCtrl = new HoatDongCtrl();
        HoatDongHocSinhCtrl m_HoatDongHSCtrl = new HoatDongHocSinhCtrl();
        HocSinhCtrl m_HocSinhCtrl = new HocSinhCtrl();

        #endregion

        #region Hàm

        private void loadDSDiem()
        {
            dsDiem.Clear();
            for (int i = 0; i <= 100; i += 10)
            {
                Diem d = new Diem();
                d.value = i;
                dsDiem.Add(d);
            }
        }
        private void loadDSHoatDong()
        {
            dsHD = m_HoatDongCtrl.layDSHoatDong();
            combTenHoatDong.DisplayMember = "TenHoatDong";
            combTenHoatDong.ValueMember = "MaHoatDong";
            combTenHoatDong.DataSource = dsHD;

        }

        private void loadDSHDHS()
        {
            if (combTenHoatDong.SelectedIndex >= 0)
            {

                string maHD = combTenHoatDong.SelectedValue.ToString();
                dsHDHS = m_HoatDongHSCtrl.layDSHD(maHD, 1);
                dataGridView_HDHS.Rows.Clear();
                int i = 0;
                foreach (HoatDongHocSinhInfo hdhs in dsHDHS)
                {
                    HoatDongInfo hd = m_HoatDongCtrl.layHoatDong(hdhs.MaHoatDong);
                    string tenhs = m_HocSinhCtrl.layTenHocSinh(hdhs.MaHocSinh);


                    dataGridView_HDHS.Rows.Add(hdhs.MaHocSinh, tenhs, hdhs.MaHoatDong, hd.TenHoatDong, null, hdhs.GhiChu);
                    DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView_HDHS.Rows[i].Cells["cmbMucDo"];
                    cell.ValueMember = "value";
                    cell.DisplayMember = "value";
                    cell.DataSource = dsDiem;
                    cell.Value = int.Parse(hdhs.MucDo);

                    i++;
                }
            }
        }
        #endregion

        private void frmQLThamGia_Load(object sender, EventArgs e)
        {
            loadDSDiem();
            loadDSHoatDong();
            loadDSHDHS();
        }

        private void btn_ThamGia_Click(object sender, EventArgs e)
        {
            if (combTenHoatDong.SelectedIndex >= 0)
            {
                string maHD = combTenHoatDong.SelectedValue.ToString();
                foreach (Control ctrl in this.Controls)
                {
                    if (ctrl.GetType() == typeof(TextBoxX))
                    {
                        TextBoxX txt = (TextBoxX)ctrl;
                        if (txt.Text.Trim() != "")
                        {
                            try
                            {
                                m_HoatDongHSCtrl.capNhatThamGia(maHD, txt.Text, 1);
                                loadDSHDHS();
                                MessageBox.Show("Cập nhật thành công");
                                txt.Text = "";
                            }
                            catch
                            {
                                MessageBox.Show("Cập nhật thất bại");
                                txt.Text = "";
                            }
                        }
                    }

                }
            }

        }

        private void btnCapNhatDiem_Click(object sender, EventArgs e)
        {
            if (dataGridView_HDHS.SelectedRows.Count > 0)
            {
                bool kt = true;
                foreach (DataGridViewRow dr in dataGridView_HDHS.SelectedRows)
                {
                    try
                    {
                        if (!dr.IsNewRow)
                        {
                            string maHS = dr.Cells["MaHocSinh"].Value.ToString();
                            string maHD = dr.Cells["MaHoatDong"].Value.ToString();
                            DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dr.Cells["cmbMucDo"];
                            m_HoatDongHSCtrl.capNhatHoatDongHS(maHS, maHD, cell.Value.ToString());
                        }
                    }
                    catch
                    {
                        kt = false;
                    }
                }
                if (kt)
                {
                    MessageBox.Show("Cập nhật điểm thành công");
                    loadDSHDHS();
                }
                else
                    MessageBox.Show("Cập nhật điểm thất bại");
            }
        }
    }
}
